package com.block.datapush.special.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.common.service.LogFactory;
import com.block.datapush.model.dto.TaskInfoDTO;
import com.block.datapush.special.service.ISpecialDataInfoService;
import com.block.db.common.result.CostomUserInfoResult;
import com.block.db.entity.MemberExtInfo;
import com.block.db.entity.SpecialDataInfo;
import com.block.db.mapper.SpecialDataInfoMapper;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2022-09-15
 */
@Service
public class SpecialDataInfoServiceImpl extends ServiceImpl<SpecialDataInfoMapper, SpecialDataInfo> implements ISpecialDataInfoService {

    private static Logger log = LogFactory.getPushLog();

    /**
     * 新增用户推送记录
     * @return
     */
    @Override
    public Long saveSpecialDataInfo(TaskInfoDTO taskInfoDTO, CostomUserInfoResult costomUserInfoResult, MemberExtInfo memberExtInfo) {

        SpecialDataInfo specialDataInfo = new SpecialDataInfo();

        specialDataInfo.setDateInfo(LocalDate.now().toString());
        specialDataInfo.setAppCode(memberExtInfo.getAppCode());
        specialDataInfo.setAppName(memberExtInfo.getAppName());
        specialDataInfo.setAppType(memberExtInfo.getAppType());

        specialDataInfo.setCostomId(taskInfoDTO.getInfoId());
        specialDataInfo.setCostomItemId(String.valueOf(taskInfoDTO.getTaskId()));
        specialDataInfo.setCostomType(taskInfoDTO.getInfoType());
        specialDataInfo.setPriceAmount(taskInfoDTO.getPriceAmount());

        if (ObjectUtil.isNotEmpty(costomUserInfoResult)){
            // 如果没有可用的用户信息，则不进行分配
            specialDataInfo.setAdminUserId(costomUserInfoResult.getAdminUserId());
            specialDataInfo.setDeptId(costomUserInfoResult.getDeptId());
        }else{
            specialDataInfo.setDeptId(taskInfoDTO.getDeptId());
        }

        specialDataInfo.setChannelName(memberExtInfo.getChannelName());
        specialDataInfo.setChannelCode(memberExtInfo.getChannelCode());

        specialDataInfo.setUid(memberExtInfo.getUid());
        specialDataInfo.setUsername(memberExtInfo.getUsername());
        specialDataInfo.setMobile(memberExtInfo.getMobile());
        specialDataInfo.setIdCard(memberExtInfo.getIdCard());
        specialDataInfo.setProvince(memberExtInfo.getProvince());
        specialDataInfo.setCity(memberExtInfo.getCity());
        specialDataInfo.setAge(memberExtInfo.getAge());
        specialDataInfo.setSex(memberExtInfo.getSex()==1?"男":"女");
        specialDataInfo.setLoanAmount(memberExtInfo.getLoanAmount());
        specialDataInfo.setHouse(memberExtInfo.getHouse());
        specialDataInfo.setCar(memberExtInfo.getCar());
        specialDataInfo.setGjj(memberExtInfo.getGjj());
        specialDataInfo.setZhimaScore(memberExtInfo.getZhimaScore());
        specialDataInfo.setBaitiao(memberExtInfo.getBaitiao());
        specialDataInfo.setHuabei(memberExtInfo.getHuabei());
        specialDataInfo.setOriginCreateTime(memberExtInfo.getCreateTime());

        boolean saveRes = this.save(specialDataInfo);
        log.info("【新增特殊菜单推送记录】新增操作完成【saveRes】{}【uid】{}【costomUserInfoResult】{}",
                saveRes, memberExtInfo.getUid(), JSON.toJSONString(costomUserInfoResult));

        if (saveRes){
            return specialDataInfo.getId();
        }

        return null;
    }
}
